package net.crisps.cloud.utils.robot_util.service.default_config;


import lombok.extern.slf4j.Slf4j;
import net.crisps.cloud.utils.common_util.CustomStrUtil;
import net.crisps.cloud.utils.robot_util.vo.WeChatRobotConfigVo;
import net.crisps.common.entity.TreeBook;
import net.crisps.common.treebook.feign.TreeBookFeignService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.stream.Collectors;

import static net.crisps.cloud.utils.common_util.CustomListUtil.toArrayList;

/**
 * @author tangchuan
 * @date 2023/11/17 15:24
 */
@Slf4j
@Component
@ConditionalOnClass(TreeBookFeignService.class)
public class DefaultTreeBookConfigService {
    @Resource
    private TreeBookFeignService treeBookFeignService;
    /**
     * 机器字典
     */
    private static final String WE_CHAT_ROBOT_TREE_BOOK_PREFIX = "robot-";
    /**
     * 项目应用id
     */
    @Value("${spring.application.name}")
    private String projectAppId;

    public WeChatRobotConfigVo getDefaultConfig() {
        TreeBook treeBook = treeBookFeignService.findTreeBookByCode(WE_CHAT_ROBOT_TREE_BOOK_PREFIX + projectAppId).getData();
        if (treeBook == null) return null;
        return WeChatRobotConfigVo.builder()
                .wechatRobotUrl(treeBook.getExt1())
                .projectName(treeBook.getName())
                .projectAppId(projectAppId)
                .notifyUserPhone(
                        toArrayList(treeBook.getExt2(), treeBook.getExt3(), treeBook.getExt4(), treeBook.getExt5())
                                .stream()
                                .filter(CustomStrUtil::isNotBlank)
                                .map(a -> toArrayList(a.split(CustomStrUtil.COMMA)))
                                .flatMap(Collection::stream)
                                .filter(CustomStrUtil::isNotBlank)
                                .distinct()
                                .collect(Collectors.toList()))
                .build();
    }


}
